using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using System.Collections;

namespace OgilvyOne.SqlServerDAL
{
    public class Label : ILabel
    {
        // Methods
        public void Delete(int label_id)
        {
            string cmdText = "UPDATE label_info SET isdel='1' WHERE Label_ID = " + label_id;
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, cmdParms);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }

        public IList getLabelList()
        {
            string cmdText = "SELECT label_id,label_name,label_desc,label_content FROM label_info where ISDEL=0";
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, cmdParms))
            {
                while (reader.Read())
                {
                    LabelInfo info = new LabelInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3));
                    list.Add(info);
                }
            }
            return list;
        }

        public LabelInfo getLabelList(int Tem_ID)
        {
            LabelInfo info = null;
            string cmdText = "SELECT label_id,label_name,label_desc,label_content FROM label_info where label_id=" + Tem_ID;
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, cmdParms))
            {
                while (reader.Read())
                {
                    info = new LabelInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3));
                }
            }
            return info;
        }

        public int getMaxLabelID()
        {
            SqlParameter[] cmdParms = new SqlParameter[0];
            string cmdText = "select max(label_id) from label_info";
            return (int)SQLHelper.ExecuteScalar(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, cmdParms);
        }

        public void Insert(LabelInfo labelinfo)
        {
            string cmdText = "INSERT INTO label_info(label_name,label_desc,label_content,isdel) VALUES('" + labelinfo.Label_Name + "', '" + labelinfo.Label_Desc + "','" + labelinfo.Label_Content + "','0')";
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, cmdParms);
                        transaction.Commit();
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                }
            }
        }

        public void Update(LabelInfo labelinfo)
        {
            string cmdText = string.Concat(new object[] { "UPDATE label_info SET label_name ='", labelinfo.Label_Name, "',label_desc = '", labelinfo.Label_Desc, "',label_content = '", labelinfo.Label_Content, "' WHERE Label_ID =", labelinfo.Label_ID });
            SqlParameter[] cmdParms = new SqlParameter[0];
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, cmdParms);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}